package net.hn.hnms.biz.cockpit.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.cockpit.domain.vo.*;
import net.hn.hnms.biz.cockpit.mapper.MineMapper;
import net.hn.hnms.biz.cockpit.mapper.ProductionManagementMapper;
import net.hn.hnms.biz.cockpit.service.IProductionManagementService;
import net.hn.hnms.biz.cockpit.utils.CalculateUtil;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@DS("jhgl")
public class ProductionManagementServiceImpl implements IProductionManagementService {

    private final ProductionManagementMapper productionManagementMapper;

    private final MineMapper mineMapper;

    @Override
    public List<ProductTrendVo> getProductTrend(List<String> xData, String beginDate, String endDate, String mineCode) {
        List<ProductTrendVo> result = Lists.newArrayList();
        // 获取计划数
        List<ProductTrendVo> planProduct = productionManagementMapper.getPlanProduct(beginDate, endDate, mineCode);
        // 获取计划产量
        List<ProductTrendVo> actualProduct = productionManagementMapper.getActualProduct(beginDate, endDate, mineCode);
        xData.forEach(item -> {
            ProductTrendVo p = new ProductTrendVo();
            ProductTrendVo productTrendVo = planProduct.stream().filter(planItem -> planItem.getProductDate().equals(item)).findFirst().orElse(null);
            ProductTrendVo productTrendVo1 = actualProduct.stream().filter(actualItem -> actualItem.getProductDate().equals(item)).findFirst().orElse(null);
            p.setProductDate(item)
                    .setPlanProduct(productTrendVo == null ? "0" : productTrendVo.getPlanProduct())
                    .setActualProduct(productTrendVo1 == null ? "0" : productTrendVo1.getActualProduct());
            result.add(p);
        });
        return result;
    }

    @Override
    public ProductStatisticsVo getProductStatistics(String mineCode) {
        ProductStatisticsVo productStatisticsVo = new ProductStatisticsVo();
        String month = DateUtils.parseDateToStr("yyyyMM", DateUtils.getNowDate());
        String year = DateUtils.parseDateToStr(DateUtils.YYYY, DateUtils.getNowDate());
        // 计划
        String planProduct = productionManagementMapper.getPlan(month, mineCode);
        String yearPlanProduct = productionManagementMapper.getPlan(year, mineCode);
        // 实际
        String actualProduct = productionManagementMapper.getActual(month, mineCode);
        String yearActualProduct = productionManagementMapper.getActual(year, mineCode);
        productStatisticsVo.setPlanProduct(planProduct)
                .setActualProduct(actualProduct)
                .setCompletionRate(CalculateUtil.getPercent(planProduct, actualProduct))
                .setPlanProductYear(yearPlanProduct)
                .setActualProductYear(yearActualProduct)
                .setCompletionRateYear(CalculateUtil.getPercent(yearPlanProduct, yearActualProduct));
        return productStatisticsVo;
    }

    @Override
    public List<ProductDetailVo> productDetail(String mineCode) {
        List<ProductDetailVo> result = mineMapper.getCurWorkFace(mineCode);
        if (CollectionUtils.isNotEmpty(result)) {
            String date = DateUtils.parseDateToStr("yyyyMMdd", DateUtils.getNowDate());
            String month = DateUtils.parseDateToStr("yyyyMM", DateUtils.getNowDate());
            String year = DateUtils.parseDateToStr(DateUtils.YYYY, DateUtils.getNowDate());
            // 计划
            String dateProduct = productionManagementMapper.getPlan(date, mineCode);
            String planProduct = productionManagementMapper.getPlan(month, mineCode);
            String yearPlanProduct = productionManagementMapper.getPlan(year, mineCode);
            // 实际
            List<String> faceIds = result.stream().map(ProductDetailVo::getWorkFaceId).collect(Collectors.toList());
            List<ProductDetailVo> workFaceActualDay = productionManagementMapper.getWorkFaceActual(date, mineCode, faceIds);
            List<ProductDetailVo> workFaceActualMonth = productionManagementMapper.getWorkFaceActual(month, mineCode, faceIds);
            List<ProductDetailVo> workFaceActualYear = productionManagementMapper.getWorkFaceActual(year, mineCode, faceIds);
            result.forEach(item -> {
                item.setPlanProduct(StringUtils.isBlank(dateProduct) ? "0" : dateProduct).setPlanProductMonth(planProduct).setPlanProductYear(yearPlanProduct);
                ProductDetailVo productDetailVo = workFaceActualDay.stream().filter(u -> StringUtils.equals(u.getWorkFaceId(), item.getWorkFaceId())).findFirst().orElse(null);
                item.setActualProduct(productDetailVo == null ? "0" : productDetailVo.getPlanProduct());
                ProductDetailVo productDetailVo1 = workFaceActualMonth.stream().filter(u -> StringUtils.equals(u.getWorkFaceId(), item.getWorkFaceId())).findFirst().orElse(null);
                item.setActualProductMonth(productDetailVo1 == null ? "0" : productDetailVo1.getPlanProduct());
                ProductDetailVo productDetailVo2 = workFaceActualYear.stream().filter(u -> StringUtils.equals(u.getWorkFaceId(), item.getWorkFaceId())).findFirst().orElse(null);
                item.setActualProductYear(productDetailVo2 == null ? "0" : productDetailVo2.getPlanProduct());
            });
        }
        return result;
    }

    @Override
    public List<DrivingDetailVo> getDrivingDetail(String mineCode) {
        List<DrivingDetailVo> result = mineMapper.getCurDrivingWorkFace(mineCode);
        if (CollectionUtils.isNotEmpty(result)) {
            String date = DateUtils.parseDateToStr("yyyyMMdd", DateUtils.getNowDate());
            String month = DateUtils.parseDateToStr("yyyyMM", DateUtils.getNowDate());
            String year = DateUtils.parseDateToStr(DateUtils.YYYY, DateUtils.getNowDate());
            List<String> faceIds = result.stream().map(DrivingDetailVo::getFaceId).collect(Collectors.toList());
            String dayDrivingPlan = productionManagementMapper.getDrivingPlan(date, mineCode);
            String monthDrivingPlan = productionManagementMapper.getDrivingPlan(month, mineCode);
            String yearDrivingPlan = productionManagementMapper.getDrivingPlan(year, mineCode);
            List<DrivingDetailVo> dayDrivingDetails = productionManagementMapper.getDrivingActual(date, mineCode, faceIds);
            List<DrivingDetailVo> monthDrivingDetails = productionManagementMapper.getDrivingActual(month, mineCode, faceIds);
            List<DrivingDetailVo> yearDrivingDetails = productionManagementMapper.getDrivingActual(year, mineCode, faceIds);
            result.forEach(item -> {
                item.setPlanDriving(StringUtils.isBlank(dayDrivingPlan) ? "0" : dayDrivingPlan).setPlanDrivingMonth(monthDrivingPlan).setPlanDrivingYear(yearDrivingPlan);
                DrivingDetailVo drivingDetailVo = dayDrivingDetails.stream().filter(u -> StringUtils.equals(u.getFaceId(), item.getFaceId())).findFirst().orElse(null);
                item.setActualDriving(drivingDetailVo == null ? "0" : drivingDetailVo.getActualDriving());
                DrivingDetailVo drivingDetailVo1 = monthDrivingDetails.stream().filter(u -> StringUtils.equals(u.getFaceId(), item.getFaceId())).findFirst().orElse(null);
                item.setActualDrivingMonth(drivingDetailVo1 == null ? "0" : drivingDetailVo1.getActualDriving());
                DrivingDetailVo drivingDetailVo2 = yearDrivingDetails.stream().filter(u -> StringUtils.equals(u.getFaceId(), item.getFaceId())).findFirst().orElse(null);
                item.setActualDrivingYear(drivingDetailVo2 == null ? "0" : drivingDetailVo2.getActualDriving());
            });
        }
        return result;
    }

    @Override
    public List<DistanceTrendVo> getDistanceTrend(List<String> xData, String beginDate, String endDate, String mineCode) {
        List<DistanceTrendVo> result = Lists.newArrayList();
        // 获取计划数
        List<DistanceTrendVo> planDistance = productionManagementMapper.getPlanDriving(beginDate, endDate, mineCode);
        // 获取计划进尺
        List<DistanceTrendVo> actualDistance = productionManagementMapper.getActualDriving(beginDate, endDate, mineCode);
        xData.forEach(item -> {
            DistanceTrendVo d = new DistanceTrendVo();
            DistanceTrendVo distanceTrendVo = planDistance.stream().filter(p -> StringUtils.equals(p.getDistanceDate(), item)).findFirst().orElse(null);
            DistanceTrendVo distanceTrendVo1 = actualDistance.stream().filter(p -> StringUtils.equals(p.getDistanceDate(), item)).findFirst().orElse(null);
            d.setDistanceDate(item).setPlanDistance(distanceTrendVo == null ? "0" : distanceTrendVo.getPlanDistance())
                    .setActualDistance(distanceTrendVo1 == null ? "0" : distanceTrendVo1.getActualDistance());
            result.add(d);
        });
        return Collections.emptyList();
    }

}
